package dfbz.homework1;
public class doudizhu{

    String[] pais = new String[54];
    String[] str1 ={"黑桃","红桃","梅花","方片"};
    String[] str2 = {"A","2","3","4","5",
            "6","7","8","9","10","J","Q","K"};

    public void getPai() {
        String[] pai = createPai();
        String[] s = xiPai(pai);
        String[][] strings = faPai(s);
        for (int i = 0; i < 3; i++) {
            System.out.print("第" +i+ "份牌为：");
            for (int j=0;j<13;j++){
                System.out.print(strings[i][j]+" ");
            }
            System.out.println();
        }

        System.out.print("剩下三张牌为：");
        for (int i = 0; i < strings[3].length; i++) {
            System.out.print(strings[3][i] + " ");
        }
    }

    /**
     * 生成牌
     */
    private String[] createPai() {
        int count = 0;
        for (int i = 1; i <= str1.length; i++) {
            for (int j = 1; j <= str2.length; j++) {
                pais[count] = str1[i-1]+str2[j-1];
                count++;
            }
        }
        pais[pais.length-2] = "小王";
        pais[pais.length-1] = "大王";
        return pais;
    }

    /**
     *
     */
    private String[] xiPai(String[] pai) {
        for (int i = 0; i < pai.length; i++) {
            // 生成一个随机的数组下标(0~53)
            int index = (int)Math.floor(Math.random() * (pai.length-1));
            // 将当前遍历到的元素与随机下标处的元素交换位置
            String tmp = pai[i];
            pai[i] = pai[index];
            pai[index] = tmp;
        }
        return pai;
    }

    /**
     *
     */
    private String[][] faPai(String[] pai) {
        String[] s1 = new String[17];
        String[] s2 = new String[17];
        String[] s3 = new String[17];
        String[] s4 = new String[3];
        int count = 0;
        for (int i=0;i<51;i+=3){
            s1[count] = pai[i];
            s2[count] = pai[i+1];
            s3[count] = pai[i+2];
            count ++;
        }
        s4[0] = pai[pai.length-1-2];
        s4[1] = pai[pai.length-1-1];
        s4[2] = pai[pai.length-1];
        String[][] ss = new String[4][];
        ss[0] = s1;
        ss[1] = s2;
        ss[2] = s3;
        ss[3] = s4;

        return ss;
    }

}
